<?php
namespace forpc\controllers;

use common\models\Word;
use Yii;
use common\components\Controller;
use common\components\PageView;
use common\models\Youdao;
use common\models\Category;

/**
 * ReciteController
 */
class TaskController extends Controller{

    public $enableCsrfValidation = false;

    public function actionWordYoudao(){

        $db = Yii::$app->db;
        $words = $db->createCommand("SELECT id FROM {{%word}} WHERE `trans`='' ORDER BY id ASC")->queryColumn();

        $data = [
            'tasks' => $words,
        ];
        return $this->renderPartial('word-youdao', $data);
    }

    public function actionAsyncWordYoudao(){
        $id = isset($_GET["id"]) ? addslashes($_GET["id"]) : 0;

        if($id <= 0){
            exit(json_encode(["code"=>-1, "info"=>"请输入单词id"], JSON_UNESCAPED_UNICODE));
        }

        $db = Yii::$app->db;
        $word = $db->createCommand("SELECT word FROM {{%word}} WHERE id=".$id)->queryScalar();

        if(empty($word)){
            exit(json_encode(["code"=>-2, "info"=>"请输入单词"], JSON_UNESCAPED_UNICODE));
        }

        try{
            $word_id = Word::synYoudao($word);
        }catch(\Exception $e){
            exit(json_encode(["code"=>-3, "info"=>$e->getMessage()], JSON_UNESCAPED_UNICODE));
        }

        if($word_id > 1){
            exit(json_encode(["code"=>1, "info"=>"成功"], JSON_UNESCAPED_UNICODE));
        }else{
            exit(json_encode(["code"=>-4, "info"=>"失败"], JSON_UNESCAPED_UNICODE));
        }
    }

    /**
     * 收藏数据处理
     * @return string
     */
    public function actionFav(){

        $db = Yii::$app->db;
        $words = $db->createCommand("SELECT `id` FROM {{%knowledge}} WHERE wid=0 AND word!='' ORDER BY id ASC")->queryColumn();
        //echo '<pre>';print_r($words);exit;

        $data = [
            'tasks' => $words,
        ];
        return $this->renderPartial('fav', $data);
    }

    public function actionAsyncFav(){
        $id = isset($_GET["id"]) ? addslashes($_GET["id"]) : 0;
//        $word = isset($_POST["word"]) ? addslashes($_POST["word"]) : "";

        if($id <= 0){
            exit(json_encode(["code"=>-1, "info"=>"请输入id"], JSON_UNESCAPED_UNICODE));
        }

        $db = Yii::$app->db;
        $word = $db->createCommand("SELECT word FROM {{%knowledge}} WHERE id=".$id)->queryScalar();

        try{
            $word_id = Word::synYoudao($word);
        }catch(\Exception $e){
            exit(json_encode(["code"=>-2, "info"=>$e->getMessage()], JSON_UNESCAPED_UNICODE));
        }

        if(intval($word_id) <= 0){
            exit(json_encode(["code"=>-3, "info"=>"有道抓取失败"], JSON_UNESCAPED_UNICODE));
        }

        $updateData = [
            'wid' => $word_id,
            'word' => '',
            'ipa_en' => '',
            'ipa_us' => '',
            'trans' => '',
            'update_time' => time(),
        ];

        $line = $db->createCommand()->update('{{%favorite}}', $updateData, 'id='.$id)->execute();
        if($line == 1){
            exit(json_encode(["code"=>1, "info"=>"成功"], JSON_UNESCAPED_UNICODE));
        }else{
            exit(json_encode(["code"=>-4, "info"=>"失败"], JSON_UNESCAPED_UNICODE));
        }
    }

}
